package br.com.tas;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;
import br.com.tas.dao.CoordinatesDao;
import br.com.tas.dao.interfaces.ICoordinatesDao;
import br.com.tas.exception.DataException;
import br.com.tas.model.CoordinatesBean;

import com.example.tas.R;

public class ListCoordinatesActivity extends ListActivity {

	private ICoordinatesDao coordinatesDao;
	List<CoordinatesBean> coordinatesBeans = null;
	static CoordinatesAdapter adapter = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		coordinatesDao = new CoordinatesDao(this);
		setContentView(R.layout.activity_list_coordinates);
	}
	
	@Override
    protected void onResume() {
    	super.onResume();
    	listCoordinates();
    }
	
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	CoordinatesBean coordinates = (CoordinatesBean) l.getItemAtPosition(position);
    	Intent intent = new Intent(ListCoordinatesActivity.this, CoordinatesActivity.class);
    	intent.putExtra("coordinates", coordinates);
    	startActivity(intent);
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list_coordinates, menu);
        return true;
    }
	
	@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	if(item.getItemId() == R.id.menu_insert_coordinates){
    		insertCoordinates();
    		return true;
    	}
    	return super.onMenuItemSelected(featureId, item);
    }
	
	private void insertCoordinates(){
    	Intent intent = new Intent(this, CoordinatesActivity.class);
    	startActivity(intent);
    }
	
	private void listCoordinates(){
	
    	try {
    		coordinatesBeans = coordinatesDao.findAll(new CoordinatesBean());
		} catch (DataException e) {
			Toast.makeText(this, R.string.error_find_address, Toast.LENGTH_SHORT).show();
		}
    	
    	if(coordinatesBeans == null){
    		coordinatesBeans = new ArrayList<CoordinatesBean>();
    	}
    	
    	final ListView lv = (ListView) findViewById(android.R.id.list);
    	lv.setAdapter(new CoordinatesAdapter(coordinatesBeans, this));
    	lv.setOnItemLongClickListener( new OnItemLongClickListener() {

			private int pos;

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View strings,
					int position, long id) {

				AlertDialog.Builder alert = new AlertDialog.Builder(
						ListCoordinatesActivity.this);
				pos = position;

				alert.setTitle(R.string.title_delete_address);
				alert.setMessage(R.string.question_delete_address);

				alert.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int whichButton) {
								try {
									//adapter = new CoordinatesAdapter(this);
									coordinatesDao.delete(coordinatesBeans.get(pos));

								} catch (DataException e) {
									e.printStackTrace();
								}
							}
				 });
				alert.setNegativeButton(R.string.button_cancel, null);

				alert.create().show();
				return false;
			}
		});
    }
}
